<?php
declare (strict_types = 1);
namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

use app\admin\middleware\Auth;
use app\common\model\Post\Cate as CateModel;
use app\common\model\Post\Post as PostModel;
use app\common\model\User\Record as RecordModel;
use think\Db;

class Analysis extends Backend
{
    protected $middleware = [
        Auth::class
    ];
    // 用户统计
    public function user()
    {

        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action == 'pie') {
                $sex0 = model('User.User')->where('sex', 0)->count();
                $sex1 = model('User.User')->where('sex', 1)->count();
                $sex2 = model('User.User')->where('sex', 2)->count();

                $auth0 = model('User.User')->where('auth', 0)->count();
                $auth1 = model('User.User')->where('auth', 1)->count();

                $data = [
                    'sex' => [$sex0, $sex1, $sex2],
                    'auth' => [$auth0, $auth1]
                ];

                $this->success('用户统计', null, $data);
            }
        }

        $navTitle = '数据统计';
        $navSmall = '用户统计';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall
        ]);
    }

    // 帖子分析
    public function post()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '', 'trim');

            //线性图数据
            if ($action == 'line') {
                $catelist = CateModel::field('id,name')->select()->toArray();
                 //提取名字
                 $name = array_column(collection($catelist)->toArray(), 'name');

                 //提取id数据
                 $ids = array_column(collection($catelist)->toArray(), 'id');

                //已采纳 /未采纳
                $Accept = null;
                $NoAccept = null;
                // halt($Accept);
                foreach ($ids as $cateid) {
                    //已采纳
                    $Accept[] = PostModel::where(['cateid' => $cateid])->whereNotNull('accept')->count();

                    //未采纳
                    $NoAccept[] = PostModel::where(['cateid' => $cateid])->whereNull('accept')->count();
                }
                
                //组装数据
                $data = [
                    'name' => $name,
                    'Accept' => $Accept,
                    'NoAccept' => $NoAccept
                ];
                // halt($data);

                $this->success('返回数据成功', null, $data);
                exit;
            }
        }
        $navTitle = '数据统计';
        $navSmall = '帖子分析';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall
        ]);
    }

    // 消费记录统计
    public function record()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '', 'trim');

            //饼图
            if ($action == 'record') {
                //获取当前年份
                $year = date("Y", time());

                $TimeList = [];

                for ($i = 1; $i <= 12; $i++) {
                    $start = "$year-$i-01";
                    $end = date("Y-m-d", strtotime("$start +1 month -1 day"));
                    $TimeList[] = [$start, $end];
                }

                $state1 = ['发布'];
                $state2 = ['采纳'];
                $state3 = ['充值'];
                $state4 = ['签到'];
                $state5 = ['商品订单'];
                $state6 = ['预约订单'];
                $state7 = ['退款'];
                foreach ($TimeList as $item) {
                    $state1[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '1'])->count();
                    $state2[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '2'])->count();
                    $state3[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '3'])->count();
                    $state4[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '4'])->count();
                    $state5[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '5'])->count();
                    $state6[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '6'])->count();
                    $state7[] = RecordModel::whereTime('createtime', 'between', $item)->where(['state' => '-1'])->count();
                }

                //X轴
                $month = ['record'];
                for ($i = 1; $i <= 12; $i++) {
                    $month[] = "{$i}月份";
                }

                //组装数据
                $data = [
                    $month,
                    $state1,
                    $state2,
                    $state3,
                    $state4,
                    $state5,
                    $state6,
                    $state7,
                ];

                $this->success('消费记录统计', null, $data);
                exit;
            }
        }
        $navTitle = '数据统计';
        $navSmall = '消费记录统计';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall
        ]);
    }
}
